package com.example.lin.day9_26.utils;


import android.support.annotation.NonNull;

import com.example.lin.day9_26.pojo.ResultData;
import com.example.lin.day9_26.pojo.User;

import java.io.File;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
import retrofit2.http.Field;

public class RetrofitClient {


    private static final class Type {

        public static final MediaType FILE_MEDIA_TYPE = MediaType.parse("application/octet-stream");


    }


    private static final String BASE_URL = "http://192.168.3.100:8080/home/";

    private Retrofit retrofit;
    private OkHttpClient okHttpClient;


    private ApiRequestService apiRequestService;
    private ApiRequestService2 apiRequestService2;
    /**
     * 连接超时时间
     */
    private int CONNECT_TIMEOUT = 15;
    /**
     * 读取数据超时时间
     */
    private int READ_TIMEOUT = 5;
    /**
     * 写出数据超时时间
     */
    private int WRITE_TIMEOUT = 5;


    private RetrofitClient() {
        okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
                .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
                .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)
                //配置OkHttp 的拦截器
//                .addInterceptor()
                //配置 cookie 缓存 , cookie 其实就是一个有规律的字符串
//                .cookieJar()
                .build();

        retrofit = new Retrofit.Builder()
                //配置OkHttpClient
                .client(okHttpClient)
                //基础服务器地址
                .baseUrl(BASE_URL)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                //Gson 转换流
                .addConverterFactory(GsonConverterFactory.create())
                //字符串转换流
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
        apiRequestService = retrofit.create(ApiRequestService.class);
    }


    public void register(String userName, String psw, Observer<ResultData> observer) {
        apiRequestService2.register(userName, psw)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    public void getUser(String userName, Observer<ResultData<User>> observer) {
        apiRequestService.getUser(userName)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    public void uploadIcon(String userId, File icon, Observer<ResultData> observer) {
        apiRequestService
                .uploadIcon(userId, MultipartBody.Part.createFormData(userId, icon.getName(), RequestBody.create(Type.FILE_MEDIA_TYPE, icon)))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }


    public void uploadFile(String userId, Map<String, File> files, Observer<ResultData> observer) {
        ArrayList<MultipartBody.Part> parts = new ArrayList<>();
        Set<Map.Entry<String, File>> entries = files.entrySet();
        for (Map.Entry<String, File> entry : entries) {
            String name = entry.getKey();
            File value = entry.getValue();
            MultipartBody.Part part = MultipartBody.Part.createFormData(name, value.getName(), RequestBody.create(Type.FILE_MEDIA_TYPE, value));
            parts.add(part);
        }
        apiRequestService
                .uploadFiles(userId, parts)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }



    public static final RetrofitClient getClient() {
        return Singleton.CLIENT;
    }

    private static final class Singleton {
        static final RetrofitClient CLIENT = new RetrofitClient();
    }


}
